﻿window.RegisterModel = User.extend({
    defaults: {
        MemLoginID: "",
        Pwd: "",
        PwdConfirm: "",
        Email: "",
        validateCode: ""
    },

    urlRoot: 'api/account/',
    url: function () {
        return this.urlRoot + 'Regist';
    }
});

window.RegisterView = Backbone.View.extend({
    el: "#jqt",
    initialize: function () {
        this.template = $('#RegisterTemplate').html();
        this.model = new RegisterModel();
    },
    events: {
        'click #registerBtn': 'userRegister',
        'click a.register_help': 'openAgreement'
    },
    render: function () {
        var partial = { header: $('#HeaderTemplate').html(), footer: $('#FooterTemplate').html(), innerFooter: $('#InnerFooterTemplate').html() };
        var data = { title: "注册", btnListR: [{ name: 'home'}] };

        this.$el.append(Mustache.render(this.template, data, partial));

        return this;
    },

    validation: function () {
        var username = $('#r_userName').val(),
            pwd = $('#r_password').val(),
            pwd2 = $('#r_password_confirm').val(),
            email = $('#r_email').val(),
            agree = $('#agreeIf')[0].checked,
            flag = true;
        $('#usernameError').text(''); $('#passwordError').text('');
        $('#passwordConfirmError').text(''); $('#emailError').text('');
        if (username == "") { $('#usernameError').text('用户名不能为空！'); flag = false; }
        if (pwd == "") { $('#passwordError').text('密码不能为空！'); flag = false; }
        if (pwd2 == "") { $('#passwordConfirmError').text('密码不能为空！'); flag = false; }
        if (pwd2 != pwd) { $('#passwordConfirmError').text('密码前后不一致！'); flag = false; }
        if (email == "") { $('#emailError').text('邮箱不能为空！'); flag = false; }
        if (email != "" && !(/^[a-zA-Z0-9]+@[a-zA-Z0-9]+.[a-z][a-z.]{2,8}$/.test(email))) { $('#emailError').text('邮箱格式错误！'); flag = false; }
        if (!agree) { flag = false; $('#agreeError').html('请先同意注册协议！'); }
        return flag;
    },

    checkUserExisted: function () {
        var username = $('#r_userName').val();
        var flag = false;
        $.ajax({
            url: 'api/account/userexist/' + username,
            dataType: 'json',
            async: false,
            success: function (res) {
                if (res != null && res == true) {
                    $('#usernameError').text('用户名已经存在！');
                    flag = true;
                }
            }
        });
        return flag;
    },

    userRegister: function () {
        var that = this;
        if (this.validation()) {
            if (!this.checkUserExisted()) {
                this.model.set({
                    MemLoginID: $('#r_userName').val(),
                    Pwd: $('#r_password').val(),
                    Email: $('#r_email').val()
                });
                this.model.save('', '', {
                    success: function (model, res) {
                        if (res && res == 202) {
                            localStorage.setItem('loginid', that.model.get('MemLoginID'));
                            var loginfrom = localStorage.getItem('loginfrom');
                            if (loginfrom) {
                                LoginView.loginAfterAction(loginfrom).call();
                            }
                            else pageView.goTo('MyUserInfo');
                        } else {
                            $('#registerError').html('注册失败！');
                        }
                    }
                });
            }
        }
    },
    openAgreement: function () {
        pageView.goTo('Agreement');
    }
});